//
//  CollectionViewController.m
//  helloword
//
//  Created by chen on 17/2/27.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "CollectionViewController.h"
#import "EventCollectionViewCell.h"

@interface CollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>{
    int COL_NUM;//每一行有几个单元格
}
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong,nonatomic) NSArray *events;
@end

@implementation CollectionViewController

- (void)viewDidLoad {
    COL_NUM = 2;
    [super viewDidLoad];
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"events" ofType:@"plist"];
    //获取属性列表文件中的全部数据
    _events = [[NSArray alloc]initWithContentsOfFile:plistPath];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -- UICollectionViewDataSource协议方法
//返回某一节中的列数（行，）
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    int num = [_events count] % COL_NUM;
    if (num == 0) {
        return [_events count]/COL_NUM;
    }else{
        return [_events count]/COL_NUM - 1;
    }
}
//提供视图中节的个数（行数，单元格）
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return COL_NUM;
}
//为某个单元格提供显示数据
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"----indexPath---%li",indexPath.section);
    EventCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    NSDictionary *array = _events[indexPath.section * COL_NUM + indexPath.row];
    cell.lable.text = [array objectForKey:@"name"];
    cell.imageView.image = [UIImage imageNamed:array[@"image"]];
//    NSLog(@"-----name:%@----image:%@----",[array objectForKey:@"name"],[array objectForKey:@"image"]);
    return cell;
}



#pragma mark -- UICollectionViewDelegate协议方法
//选择单元格之后触发
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *event = _events[indexPath.section * COL_NUM + indexPath.row];
    NSLog(@"select event name : %@",event[@"name"]);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
